<!doctype html>
<html>
<head>
    <title>RLS Testing</title>
</head>
<body>

<script src="../../../../build/yui/yui-rls-debug.js"></script>
<script src="baz.js"></script>
<script>
var max = 1,
    results = [];

var check = function() {
    if (results.length === max) {
        YUI().use('test', function(Y) {
            var s = new Y.Test.Suite("Dynamic Use");
            var cases = {
                name: 'Local RLS Tests'
            };
            Y.each(results, function(v) {
                var name = v.name.replace(/ /g, '_').replace(/#/, '').toLowerCase();
                cases[name] = (function(v) {
                    return function() {
                        Y.Assert.areEqual(0, v.missed.length, v.name + ' has missing LOADED modules: [' + Y.Object.keys(Y.Array.hash(v.missed)) + ']');
                        Y.Assert.areEqual(0, v.m, v.name + ' has missing ATTACHED modules');
                    };
                })(v);
            });
            s.add(new Y.Test.Case(cases));
            Y.Test.Runner.add(s);
            Y.Test.Runner.run();
        });
    }
};

var has = function(str, Y, mod) {
    var a = [], m = [];
    if (!(mod instanceof Array)) {
        mod = [mod];
    }
    for (var i = 0; i < mod.length; i++) {
        var k = mod[i];
        if (Y.Env._attached[k]) {
            a.push(k);
        } else {
            m.push(k);
        }
    }

    var res = {
        name: 'Test ' + str,
        missed: Y.Env._missed,
        m: m
    };

    if (res.missed.length) {
        console.error(str, 'Missing LOADED modules', res.missed);
    }

    if (res.m.length) {
        console.error(str, 'Missing ATTACHED modules', res.missed);
    }

    if (!res.m.length && !res.missed.length) {
        console.info(str, ' is OK!');
    }
    
    results.push(res);
    //check();
};

var rls_base = 'http://localhost:3000/load?';

YUI({
    rls_base: rls_base
}).use('node', 'yql', function(Y) {
    has('#1', Y, ['node-base', 'yql']);
});

YUI({
    rls_base: rls_base
}).use('dd', function(Y) {
    has('#2', Y, 'dd-ddm');
});
YUI({
    rls_base: rls_base
}).use('yql', 'node', function(Y) {
    has('#3', Y, ['yql', 'node-base']);
});

YUI({
    rls_base: rls_base
}).use('node', function(Y) {
    has('#4', Y, ['node-base']);
});
YUI({
    rls_base: rls_base,
    modules: {
        foo: {
            fullpath: 'foo.js',
            requires: ['io-base', 'querystring-stringify-simple'] //querystring-stringify-simple should not be here in 3.4.0!
        },
        bar: {
            fullpath: 'bar.js',
            requires: ['yql']
        }
    }
}).use('foo', function(Y) {
    has('#5', Y, ['foo', 'io-base']);
});

YUI({
    rls_base: rls_base,
    modules: {
        foo: {
            fullpath: 'foo.js',
            requires: ['io-base', 'querystring-stringify-simple'] //querystring-stringify-simple should not be here in 3.4.0!
        },
        bar2: {
            fullpath: 'bar2.js',
            requires: ['yql', 'foo']
        }
    }
}).use('bar2', function(Y) {
    //console.log('#6 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#6', Y, ['bar2', 'yql', 'foo', 'io-base']);
});

YUI({
    rls_base: rls_base,
    modules: {
        foo: {
            fullpath: 'foo.js',
            requires: ['io-base', 'querystring-stringify-simple'] //querystring-stringify-simple should not be here in 3.4.0!
        },
        bar: {
            fullpath: 'bar.js',
            requires: ['yql', 'foo']
        }
    }
}).use('baz', function(Y) {
    //console.log('#7 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#7', Y, ['baz', 'console']);
});

YUI({
    rls_base: rls_base
}).use('gallery-torelativetime', function(Y) {
    //console.log('#8 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#8', Y, ['gallery-torelativetime', 'loader-base']);
});

YUI({
    rls_base: rls_base
}).use('autocomplete', function(Y) {
    //console.log('#9 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#9', Y, ['autocomplete-base', 'autocomplete-list', 'autocomplete-plugin']);
});

YUI({
    rls_base: rls_base
}).use('widget', function(Y) {
    //console.log('#10 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#10', Y, ['widget', 'base-base']);
});

YUI({
    rls_base: rls_base,
    combine: true
}).use('yui2-datatable', 'dd', function(Y) {
    //console.log('#11 keys:', Y.Object.keys(Y.Env._attached).sort());
    has('#11', Y, ['dd-drag', 'yui2-datatable']);
});

</script>
</body>
</html>
